//
//  LHStatusToolBar.swift
//  sina_weibo
//
//  Created by 刘刘海强 on 16/5/13.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit

class LHStatusToolBar: UIView {
    
    private lazy var retweetButton:UIButton = self.addChildButton("timeline_icon_retweet")
    private lazy var commentButton:UIButton = self.addChildButton("timeline_icon_comment")
    private lazy var unlikeButton: UIButton = self.addChildButton("timeline_icon_unlike")
    
    var statusViewModel: LHStatusViewModel? {
        didSet {
            retweetButton.setTitle("\(statusViewModel?.retweetCount ?? "")", forState: .Normal)
            commentButton.setTitle("\(statusViewModel?.commentCount ?? "")", forState: .Normal)
            unlikeButton.setTitle("\(statusViewModel?.unlikeCount ?? "")", forState: .Normal)
            
//            retweetButton.setTitle("\(statusViewModel?.status?.reposts_count ?? 0)", forState: .Normal)
//            commentButton.setTitle("\(statusViewModel?.status?.comments_count ?? 0)", forState: .Normal)
//            unlikeButton.setTitle("\(statusViewModel?.status?.attitudes_count ?? 0)", forState: .Normal)
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        let firstLineImageView = addLineImageView()
        let SecondLineImageView = addLineImageView()
        
        //设置约束
        retweetButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        commentButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(retweetButton.snp_trailing)
            make.width.equalTo(unlikeButton)
        }
        unlikeButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.trailing.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
        }
        firstLineImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton.snp_trailing)
            make.centerY.equalTo(self)
        }
        SecondLineImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton.snp_trailing)
            make.centerY.equalTo(self)
        }
        
    }
    
    private func addChildButton(imageName: String) -> UIButton {
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        
        //去掉按钮的高亮状态
        //方法一
//        button.backgroundColor = UIColor(patternImage: UIImage(named: "timeline_card_bottom_background")!)
        //方法二
        button.adjustsImageWhenHighlighted = false

        addSubview(button)
        return button
    }
    
    private func addLineImageView() ->UIImageView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }

}
